package com.zx.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DeleteStudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 删除某个学生信息

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        Map<String, Object> map = new HashMap<>();

        if(req.getParameter("id")==""){
            map.put("isExist", false);
            ObjectMapper om = new ObjectMapper();
            String s = om.writeValueAsString(map);
            resp.getWriter().print(s);
        }else{
            int id = Integer.parseInt(req.getParameter("id"));
            if(StudentService.deleteStudent(id)){
                map.put("isExist", true);
                ObjectMapper om = new ObjectMapper();
                String s = om.writeValueAsString(map);
                resp.getWriter().print(s);
            }else{
                map.put("isExist", false);
                ObjectMapper om = new ObjectMapper();
                String s = om.writeValueAsString(map);
                resp.getWriter().print(s);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
